home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / mui / modula / demo / listdemo.mod < prev    next >
Text File  |  1996-02-07  |  10KB  |  267 lines

  1. MODULE ListDemo;
  2.  
  3. (* This little demo will show, how to write a MUI-Application using
  4. ** a list object and how to manage to set up the columns with a hook.
  5. **
  6. ** We will implement just a list and put some things into it.
  7. **
  8. ** Written 20.12.1993 by Christian 'Kochtopf' Scholz
  9. ** Email : ruebe@pool.informatik.rwth-aachen.de
  10. **   or, if pool does not work, try
  11. **   ruebe@tschil.informatik.rwth-aachen.de
  12. **
  13. ** Updated Nov 27, 1995 by Olaf Peters:
  14. **  - does not use MUIOBSOLETE tags any longer
  15. **  - uses "the ideal input loop for an object oriented MUI application"
  16. **      (see MUI_Application.doc/MUIM_Application_NewInput)
  17. *)
  18.  
  19. (* 
  20. ** First some imports
  21. *)
  22.  
  23. (*$ StackChk:=FALSE *)
  24.  
  25. IMPORT  MD:MuiD;
  26. IMPORT  MM:MuiMacros;
  27. IMPORT  ExecD;
  28. FROM    MuiSupport  IMPORT DoMethod, DOMethod, fail;
  29. FROM    UtilityD    IMPORT HookPtr, Hook, tagEnd;
  30. FROM    DosD        IMPORT ctrlC ;
  31. FROM    ExecL       IMPORT Wait;
  32. FROM    Storage     IMPORT ALLOCATE, DEALLOCATE;
  33. FROM    SYSTEM      IMPORT TAG, ADR, LONGSET, ADDRESS, CAST;
  34. FROM    MuiMacros   IMPORT set, MakeHook, NoteClose, MakeID,
  35.                            (* These are for the list !! *)
  36.                            STRING, STRPTR, STRARR, STRARRPTR;
  37.  
  38. (*
  39. ** Types
  40. *)
  41.  
  42. TYPE    tagbuffer   = ARRAY[0..30] OF LONGINT;  (* for the taglists *)
  43.         (* Now the type for the list we want to have displayed *)
  44.         ListPtr     = POINTER TO ListContents;
  45.         (* we want 3 columns *)
  46.         ListContents  = RECORD
  47.                             Column1 : STRING;
  48.                             Column2 : STRING;
  49.                             Column3 : STRING;
  50.                         END;
  51.         EntryArr      = ARRAY[0..5] OF ListPtr;
  52.  
  53.  
  54.  
  55. (*
  56. ** Lets define some CONST for some Objects
  57. *)
  58.  
  59. CONST   True =1;
  60.         False=0;
  61.  
  62.         (* Lets define some entries for our little list.
  63.         ** normally you would do this by creating a new node of type ListContents
  64.         ** e.g. with ALLOCATE(newptr, SIZE(ListContents)) where newptr is defined
  65.         ** as ListPtr. Then you have to fill this new record and pass it to
  66.         ** MUIListInsert. But beware that you have to pass a pointer to a pointer.
  67.         ** So you have to write
  68.         ** DoMethod(list, TAG(buffer, MD.mmListInsert, ADR(newptr), 
  69.         **                              1, MD.mvListInsertBottom));
  70.         **
  71.         ** So remember to pass the type POINTER to ListPtr. ( or ADR(ListPtr) )
  72.         *)
  73.  
  74.  
  75.         e1   =ListContents{Column1 : "this",
  76.                          Column2 : "is the",
  77.                          Column3 : "first"};
  78.         e2   =ListContents{Column1 : "this",
  79.                          Column2 : "is the",
  80.                          Column3 : "second"};
  81.         e3   =ListContents{Column1 : "yeah!",
  82.                          Column2 : "the",
  83.                          Column3 : "third"};
  84.         e4   =ListContents{Column1 : "and this",
  85.                          Column2 : "is the",
  86.                          Column3 : "fourth"};
  87.         e5   =ListContents{Column1 : "last",
  88.                          Column2 : "but not",
  89.                          Column3 : "least"};
  90.  
  91.         (* This is an entry with 5 elements of ListPtr.
  92.         ** we have to give a pointer to this array to the ListInsert-Method.
  93.         *)
  94.         entries = EntryArr{ADR(e1),ADR(e2),ADR(e3),ADR(e4),ADR(e5),NIL};
  95.  
  96.  
  97. (*
  98. ** Now some Variables for some objects
  99. *)
  100.  
  101. VAR     app, window, list,
  102.         listview            : MD.APTR;
  103.         msg                 : LONGINT;
  104.         signals             : LONGSET;
  105.         running             :=BOOLEAN{TRUE};
  106.         buffer, buffer1     : tagbuffer;
  107.         DspHook             : HookPtr;          (* this is for my display-hook *)
  108.  
  109. (* Lets define some lovely hook-functions *)
  110.  
  111.  
  112. PROCEDURE DspFunc(hook : HookPtr; array : MD.APTR; listPtr : MD.APTR) : MD.APTR;
  113.     BEGIN
  114.     
  115.         (* if listPtr is NIL then we have to provide the titles *)
  116.         IF listPtr#NIL THEN (* normal entry *)
  117.         
  118.         (* Yeah! Wild casts ;-)
  119.         ** What we do here is to convert some strings to the format used
  120.         ** by C.
  121.         ** In C string-arrays are defined the following way :
  122.         **      array -> elem1 -> "string1"
  123.         **               elem2 -> "string2"
  124.         **                 .
  125.         **                 .
  126.         **                 .
  127.         ** So we have a pointer called 'array' which points to an array.
  128.         ** This array consists of pointers to strings.
  129.         ** Now we have to build that in M2.
  130.         ** The type of elem1 is STRPTR (a pointer to a string)
  131.         ** The type of array is STRARRPTR ( a pointer to a string array)
  132.         ** So look in MuiMacros.def where STRPTR, STRARR and STRARRPTR are defined.
  133.         ** So the array we get passed to our hook is an C-Array. So we have
  134.         ** to cast it to STRARRPTR, since it is one of these.
  135.         ** This is done by CAST(STRARRPTR, array).
  136.         ** Since this is a pointer we have to derefernce it with ^.
  137.         ** Then we have an array of type STRARR.
  138.         ** There we have to fill in the right pointers to our columns.
  139.         ** This is done by [0], [1] and [2].
  140.         ** The second CAST in a line casts the MD.APTR which holds the pointer to
  141.         ** the element, we have to display to our real List-Type (here ListPtr).
  142.         ** Then we can dereference our ColumnX and fill in the address of it
  143.         ** in the array, which we get from MUI to fill in our strings.
  144.         ** Ofcourse we just fill in the pointer to the string (ADR).
  145.         ** (another way to do this is defining the ListContents-Type as follows :
  146.         ** TYPE ListContent = RECORD
  147.         **                      col1    : STRPTR;
  148.         **                      col2    : STRPTR;
  149.         **                      ...
  150.         ** The we do not need the ADR but must fill in directly the address of
  151.         ** the string.) Clear ?
  152.         ** (or just copy this code and modify it ;-)
  153.         *)
  154.  
  155.             CAST(STRARRPTR, array)^[0]:= ADR(CAST(ListPtr, listPtr)^.Column1);
  156.             CAST(STRARRPTR, array)^[1]:= ADR(CAST(ListPtr, listPtr)^.Column2);
  157.             CAST(STRARRPTR, array)^[2]:= ADR(CAST(ListPtr, listPtr)^.Column3);
  158.         
  159.         ELSE (* return titles ( centered, underlined ) *)
  160.             CAST(STRARRPTR, array)^[0]:= ADR("\033n\0333\033c\033uErste Spalte");
  161.             CAST(STRARRPTR, array)^[1]:= ADR("\033c\0333\033uZweite Spalte");
  162.             CAST(STRARRPTR, array)^[2]:= ADR("\033c\0333\033uDritte Spalte");
  163.         END;
  164.         RETURN 0; (* dummy return value *)
  165.     END DspFunc;
  166.  
  167.  
  168.  
  169. BEGIN
  170.     MakeHook(DspFunc, DspHook);             (* generate the hook *)
  171.  
  172.     (* lets create some objects *)
  173.  
  174.     list:=MM.ListObject(TAG(buffer,
  175.                 MD.maFrame,                 MD.mvFrameInputList,
  176.                 MD.maListDisplayHook,       DspHook,            (* here put the hook *)
  177.                 (* our format string *)
  178.                 MD.maListFormat,            ADR("P=\033c\033b D=8,P=\033c,P=\033c"),
  179.                 80423E66H,                  TRUE,               (* missing in 1.4 *)
  180.                 (*MD.maListTitle,             TRUE,               (* give it titles *)*)
  181.                tagEnd));
  182.     listview:=MM.ListviewObject(TAG(buffer,
  183.                 MD.maListviewList,          list,               (* put here our list *)
  184.                 tagEnd));
  185.  
  186.     (* now the window *)
  187.  
  188.     window:=MM.WindowObject(TAG(buffer,
  189.                 MD.maWindowTitle,       ADR("List-Demonstration"),
  190.                 MD.maWindowID,          MakeID("LDEM"),
  191.                 MM.WindowContents,
  192.                         MM.VGroup(TAG(buffer1,
  193.                             MD.maFrame,                 MD.mvFrameGroup,
  194.                             MD.maFrameTitle,            ADR("Do the list, err, twist"),
  195.                             MM.Child,                   listview,       (* just our list *)
  196.                             tagEnd)),
  197.                 tagEnd));
  198.  
  199.     app:=MM.ApplicationObject(TAG(buffer,
  200.                        MD.maApplicationTitle,      ADR("List-Demonstration"),
  201.                        MD.maApplicationAuthor,     ADR("Christian Scholz"),
  202.                        MD.maApplicationVersion,    ADR("$VER: ListDemo V0.1 (20.12.1993)"),
  203.                        MD.maApplicationCopyright,  ADR("© 1993 by Christian 'Kochtopf' Scholz"),
  204.                        MD.maApplicationDescription,ADR("Shows how to implement a list with MUI"),
  205.                        MD.maApplicationBase,       ADR("LD"),
  206.                        MM.SubWindow,               window,
  207.                        tagEnd));
  208.     IF app=NIL THEN fail(app,"failed to create app"); END;
  209.  
  210.     (* some notification *)
  211.  
  212.     NoteClose(app,window,MD.mvApplicationReturnIDQuit);
  213.     
  214.     (* set up the cycle chain *)
  215.  
  216.     set(listview, MD.maCycleChain, LONGINT(TRUE)) ;
  217.  
  218.     (* set the active object *)
  219.  
  220.     set(window, MD.maWindowActiveObject, listview);
  221.  
  222.     (* open the window *)
  223.  
  224.     set(window, MD.maWindowOpen, True);
  225.  
  226.  
  227.     set(list, MD.maListQuiet, True);    (* that you don't see updating *)
  228.  
  229.     (* Insert something in the list
  230.     ** Here we call mmListInsert with an array of ListPtr, which shall be
  231.     ** inserted in our list. This array must be terminated with a NIL
  232.     ** and the count-value must be -1. (refer to autodocs)
  233.     *)
  234.  
  235.     DoMethod(list, TAG(buffer,
  236.             MD.mmListInsert, ADR(entries), -1, MD.mvListInsertBottom));
  237.  
  238.     (* and again! *)
  239.  
  240.     DoMethod(list, TAG(buffer,
  241.             MD.mmListInsert, ADR(entries), -1, MD.mvListInsertBottom));
  242.  
  243.  
  244.     set(list, MD.maListQuiet, False);   (* show the list *)
  245.  
  246.  
  247.     (* Main loop *)
  248.  
  249.     signals := LONGSET{} ;
  250.  
  251.     LOOP
  252.       IF DOMethod(app, TAG(buffer, MD.mmApplicationNewInput, ADR(signals))) = MD.mvApplicationReturnIDQuit THEN EXIT END ;
  253.  
  254.       IF signals # LONGSET{} THEN
  255.         INCL(signals, ctrlC) ;
  256.         signals := Wait(signals) ;
  257.         IF ctrlC IN signals THEN EXIT END ;
  258.       END (* IF *) ;
  259.     END (* WHILE *) ;
  260.  
  261.     set(window,MD.maWindowOpen,False);          (* close window *)
  262.  
  263.     fail(app,"");                               (* say bye to the application *)
  264.  
  265. END ListDemo.
  266.  
  267.